package com.ymatou.iapi.liveseller.testcase;

import java.util.Date;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.google.gson.JsonObject;
import com.ymatou.iapi.liveseller.parameter.CreateSellerLiveBean;
import com.ymatou.iapi.liveseller.parameter.GetUnExplainProductListBean;
import com.ymatou.iapi.liveseller.service.GetUnExplainProductListCall;
import com.ymatou.iapi.productmanager.parameter.product.AddSellerProductRequest;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.LiveRoomCallService;
import com.ymttest.business.service.ProductManagerCallServiceV2;
import com.ymttest.database.model.LiveRoom;
import com.ymttest.database.model.livetv.ProductVideoExplain;
import com.ymttest.database.sqlwapper.LiveRoomWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.YMTDateUtil;
/**
*待讲解商品列表Test
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_GetUnExplainProductList{
	private static GetUnExplainProductListBean getunexplainproductlistBean;
	private static GetUnExplainProductListCall getunexplainproductlistCall;
	private static LiveRoomWapper liveRoomWapper;
	private static int sellerId = Integer.parseInt(EnvSetup.getData("autotest_seller1"));
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("待讲解商品列表Test");
	}

	@Before
	public void caseUp() {
		getunexplainproductlistBean=new GetUnExplainProductListBean();
		getunexplainproductlistCall=new GetUnExplainProductListCall();
		liveRoomWapper=new LiveRoomWapper();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(getunexplainproductlistCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetUnExplainProductList_001() {
		Logger.start(true,"求讲解一次，查看待讲解列表");
		try {
			AddSellerProductRequest addpro = ProductManagerCallServiceV2.initAddSellerProductRequest(sellerId);
			String pid = ProductManagerCallServiceV2.addNewProduct(addpro).get("Data").getAsJsonObject().get("ProductId").getAsString();
			Logger.comment("创建商品：" + pid);
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(LiveRoomCallService.initCreatesellerliveBean(sellerId));
			LiveRoomCallService.AddProductsToLive(createlive.getId(), createlive.getUserId(), pid);
			LiveRoomCallService.SellerLiveOnline(createlive.getId(), createlive.getUserId());
			//求讲解
			LiveRoomCallService.AskProductVideo(createlive.getId(), pid, 1);
			getunexplainproductlistBean.setLiveId(createlive.getId());
			getunexplainproductlistBean.setIsTimeOrExplainNum(true);
			getunexplainproductlistCall.setData(getunexplainproductlistBean);
			getunexplainproductlistCall.callService();			
			Logger.verifyEquals("200", getunexplainproductlistCall.getString("Code"), "验证code");
			JsonObject Explain = getunexplainproductlistCall.getJsonObject().getAsJsonObject("Data").getAsJsonArray("UnExplainProductList").get(0).getAsJsonObject();
			ProductVideoExplain pve = liveRoomWapper.selectProductvideoExplain(pid);
			Logger.verifyEquals(pve.getProductId(),Explain.get("ProductId").getAsString(), "验证 ProductId");
			Logger.verifyEquals(pve.getExplainStatus(),0, "验证ExplainStatus");
			Logger.verifyEquals(pve.getExplainNum(),Explain.get("ExplainNum").getAsInt(), "验证ExplainNum");
			Logger.verifyEquals(pve.getTaskId(),0, "验证TaskId");
			Logger.verifyEquals(pve.getLiveId(),createlive.getId(), "验证LiveId");
			Logger.verifyEquals(pve.getVideoFinishTime(),null, "验证VideoFinishTime");
			Logger.verifyEquals(pve.getLastExplainTime().getTime(),Explain.get("LastExplainTime").getAsLong(), "验证LastExplainTime");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetUnExplainProductList_002() {
		Logger.start(true,"求讲解，查看待讲解列表按照关注度排序");
		try {
			AddSellerProductRequest addpro = ProductManagerCallServiceV2.initAddSellerProductRequest(sellerId);
			String pid = ProductManagerCallServiceV2.addNewProduct(addpro).get("Data").getAsJsonObject().get("ProductId").getAsString();
			String pid1 = ProductManagerCallServiceV2.addNewProduct(addpro).get("Data").getAsJsonObject().get("ProductId").getAsString();
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(LiveRoomCallService.initCreatesellerliveBean(sellerId));
			LiveRoomCallService.AddProductsToLive(createlive.getId(), createlive.getUserId(), pid);
			LiveRoomCallService.AddProductsToLive(createlive.getId(), createlive.getUserId(), pid1);
			LiveRoomCallService.SellerLiveOnline(createlive.getId(), createlive.getUserId());
			//求讲解
			LiveRoomCallService.AskProductVideo(createlive.getId(), pid, 1);
			LiveRoomCallService.AskProductVideo(createlive.getId(), pid, 2);
			LiveRoomCallService.AskProductVideo(createlive.getId(), pid1, 3);
			getunexplainproductlistBean.setLiveId(createlive.getId());
			getunexplainproductlistBean.setIsTimeOrExplainNum(false);
			getunexplainproductlistCall.setData(getunexplainproductlistBean);
			getunexplainproductlistCall.callService();			
			Logger.verifyEquals("200", getunexplainproductlistCall.getString("Code"), "验证code");
			JsonObject Explain = getunexplainproductlistCall.getJsonObject().getAsJsonObject("Data").getAsJsonArray("UnExplainProductList").get(0).getAsJsonObject();
			ProductVideoExplain pve = liveRoomWapper.selectProductvideoExplain(pid);
			Logger.verifyEquals(pve.getProductId(),Explain.get("ProductId").getAsString(), "验证 ProductId");
			Logger.verifyEquals(pve.getExplainStatus(),0, "验证ExplainStatus");
			Logger.verifyEquals(pve.getExplainNum(),Explain.get("ExplainNum").getAsInt(), "验证ExplainNum");
			Logger.verifyEquals(pve.getTaskId(),0, "验证TaskId");
			Logger.verifyEquals(pve.getLiveId(),createlive.getId(), "验证LiveId");
			Logger.verifyEquals(pve.getVideoFinishTime(),null, "验证VideoFinishTime");
			Logger.verifyEquals(pve.getLastExplainTime().getTime(),Explain.get("LastExplainTime").getAsLong(), "验证LastExplainTime");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetUnExplainProductList_003() {
		Logger.start(true,"求讲解，查看待讲解列表按照时间排序");
		try {
			AddSellerProductRequest addpro = ProductManagerCallServiceV2.initAddSellerProductRequest(sellerId);
			String pid = ProductManagerCallServiceV2.addNewProduct(addpro).get("Data").getAsJsonObject().get("ProductId").getAsString();
			String pid1 = ProductManagerCallServiceV2.addNewProduct(addpro).get("Data").getAsJsonObject().get("ProductId").getAsString();
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(LiveRoomCallService.initCreatesellerliveBean(sellerId));
			LiveRoomCallService.AddProductsToLive(createlive.getId(), createlive.getUserId(), pid);
			LiveRoomCallService.AddProductsToLive(createlive.getId(), createlive.getUserId(), pid1);
			LiveRoomCallService.SellerLiveOnline(createlive.getId(), createlive.getUserId());
			//求讲解
			LiveRoomCallService.AskProductVideo(createlive.getId(), pid1, 2);
			LiveRoomCallService.AskProductVideo(createlive.getId(), pid1, 3);
			Thread.sleep(1000);
			LiveRoomCallService.AskProductVideo(createlive.getId(), pid, 1);
			getunexplainproductlistBean.setLiveId(createlive.getId());
			getunexplainproductlistBean.setIsTimeOrExplainNum(true);
			getunexplainproductlistCall.setData(getunexplainproductlistBean);
			getunexplainproductlistCall.callService();			
			Logger.verifyEquals("200", getunexplainproductlistCall.getString("Code"), "验证code");
			JsonObject Explain = getunexplainproductlistCall.getJsonObject().getAsJsonObject("Data").getAsJsonArray("UnExplainProductList").get(0).getAsJsonObject();
			ProductVideoExplain pve = liveRoomWapper.selectProductvideoExplain(pid);
			Logger.verifyEquals(pve.getProductId(),Explain.get("ProductId").getAsString(), "验证 ProductId");
			Logger.verifyEquals(pve.getExplainStatus(),0, "验证ExplainStatus");
			Logger.verifyEquals(pve.getExplainNum(),Explain.get("ExplainNum").getAsInt(), "验证ExplainNum");
			Logger.verifyEquals(pve.getTaskId(),0, "验证TaskId");
			Logger.verifyEquals(pve.getLiveId(),createlive.getId(), "验证LiveId");
			Logger.verifyEquals(pve.getVideoFinishTime(),null, "验证VideoFinishTime");
			Logger.verifyEquals(pve.getLastExplainTime().getTime(),Explain.get("LastExplainTime").getAsLong(), "验证LastExplainTime");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}